{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a78bdc75-36ef-44fb-9a4f-fdc081598c0e",
   "metadata": {},
   "source": [
    "# Python基本语法练习\n",
    "\n",
    "- 作用：用于练习Python的基本语法\n",
    "- 作者：xiaopan, xpgo@163.com\n",
    "- 使用：使用Jupyter Notebook逐段运行代码学习相关语法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0806002d-e51a-4b87-8f8d-412c4052c6a5",
   "metadata": {},
   "source": [
    "## 1. Python的入门代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12d49240-62dc-4545-ab54-62e79b17988f",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "print(\"Hello world!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78f96775-428a-4395-87cd-798701216ef9",
   "metadata": {},
   "source": [
    "## 2. 练习Python中不同的变量类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1abb2e52-2058-4ec0-b18e-4d63f9d1dae8",
   "metadata": {},
   "outputs": [],
   "source": [
    "webpage_url = 'http://vis-www.cs.umass.edu/lfw/alpha_last_A.html'  # 字符串变量\n",
    "image_number = 100  # 整数型变量\n",
    "image_scale = 1.5  # 浮点数变量\n",
    "image_png_only = True  # 逻辑变量\n",
    "\n",
    "print(\"I will download \" + str(image_number) + \" images from \" + webpage_url)\n",
    "print(\"Only download PNG images: \" + str(image_png_only))\n",
    "print(\"The images will be scaled to \" + str(image_scale) + \" of original size.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9900569-4f46-496f-8da6-d5ea35470955",
   "metadata": {},
   "source": [
    "### 改变变量值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81789dc2-7a39-48bf-bd24-889c2d570317",
   "metadata": {},
   "outputs": [],
   "source": [
    "webpage_url = 'http://www.baidu.com'\n",
    "image_number = 50\n",
    "image_scale = 0.5\n",
    "image_png_only = False\n",
    "\n",
    "print(\"I will download \" + str(image_number) + \" images from \" + webpage_url)\n",
    "print(\"Only download PNG images: \" + str(image_png_only))\n",
    "print(\"The images will be scaled to \" + str(image_scale) + \" of original size.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6293fd6f-3578-42f1-815f-105902965c02",
   "metadata": {},
   "source": [
    "## 3. 练习字符串类型的定义方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "112aed4e-f9fe-4652-a854-d08c358c40f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "webpage_url = 'http://www.baidu.\\n com'\n",
    "webpage_description = '这是百度网址，单引号里可用\"双引号\"，还可以用回车符：\\n，表示是新的一行。'\n",
    "image_name = \"car.png\"\n",
    "image_description = '''\n",
    "    这是图片描述。\n",
    "    这里都是字符串的内容\n",
    "    可以随便换行！\n",
    "'''\n",
    "\n",
    "print(\"网页地址：\" + webpage_url)\n",
    "print(\"网址说明：\" + webpage_description)\n",
    "print(\"图片名称：\" + image_name)\n",
    "print(\"图片描述：\" + image_description)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a531829-0005-475b-aa71-4b8fae53d267",
   "metadata": {},
   "source": [
    "## 4. 练习字符串对象的函数操作、字符串子范围的选取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fa631ba-863d-4adc-b8be-abd79741d3bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "webpage_url = 'http://www.baidu.com'\n",
    "webpage_description = '这是百度网址，单引号里可用\"双引号\"，还可以用回车符：\\n这是新的一行。'\n",
    "\n",
    "url_upper = webpage_url.upper()\n",
    "url_baidu_position = webpage_url.find(\"baidu\")\n",
    "url_length = len(webpage_url)\n",
    "url_part5 = webpage_url[0:5]\n",
    "url_head2 = webpage_url[1]\n",
    "url_last3 = webpage_url[-3:]\n",
    "\n",
    "print(\"网址：\" + webpage_url)\n",
    "print(\"网址大写：\" + url_upper)\n",
    "print(\"网址中baidu的位置：\" + str(url_baidu_position))\n",
    "print(\"网址长度：\" + str(url_length))\n",
    "print(\"网址前5个字符：\" + url_part5)\n",
    "print(\"网址第2个字符：\" + url_head2)\n",
    "print(\"网址最后3个字符：\" + url_last3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f29808b4-3f14-4748-8183-236167e67155",
   "metadata": {},
   "source": [
    "## 5. 练习数值型变量的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffbc7bba-0d12-40dc-881d-ea15b53eb2eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 4\n",
    "c = a * a + b * b\n",
    "pi = 3.1415926\n",
    "pi_int = int(pi)  # 转换成整数\n",
    "\n",
    "# 不同形式的输出\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(pi)\n",
    "print(pi_int)\n",
    "print(\"pi = \" + str(pi))\n",
    "print(\"pi = {:5.2f}\".format(pi))  # 5位数，小数点后2位"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efb128e5-b127-4179-b020-e9c418dad4cd",
   "metadata": {},
   "source": [
    "## 6. 练习列表的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eb1e5b1-9429-4d07-b3af-7de2929afe1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\"]\n",
    "print(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6236a25c-c61b-460b-94b2-92e338c66c65",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_names.append(\"image5.png\")\n",
    "print(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "807a1164-e1cd-4e75-82a9-93ec8cb6fc06",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "idx = image_names.index(\"image2.png\")\n",
    "print(idx)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d7c571c-1309-4e97-b193-efed29994d8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_count = len(image_names)\n",
    "print(image_count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3fd8b40-01ed-4a30-bbee-52f6c7beb9ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "del image_names[2]\n",
    "print(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f618413e-d661-457f-9aaa-9240d376439b",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_names2 = [\"image6.png\", \"image7.jpg\"]\n",
    "image_names = image_names + image_names2\n",
    "print(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7539f3f0-d6bb-4193-bb89-5f0da405fd52",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_info = [\"PNG\", 400, 800, True]\n",
    "print(image_info[2])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "723f0cf1-cb49-45c1-be68-2fb8e9acf2fb",
   "metadata": {},
   "source": [
    "## 7. 练习字典的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76be26e3-81d4-4fc1-9522-398d135e9070",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_info = [\"image1.png\", \"PNG\", 400, 800, True]  # 定义列表\n",
    "print(image_info[1])  # PNG\n",
    "print(image_info[-1])  # True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9722fba1-2dfc-4b52-a2cc-495673f2144d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_info = {\n",
    "    \"name\": \"image1.png\",\n",
    "    \"width\": 400,\n",
    "    \"height\": 800,\n",
    "    \"isPNG\": True,\n",
    "}  # 定义字典\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6618938-74ee-4302-b57c-e54c1a37333a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "print(image_info[\"name\"])  # image1.png\n",
    "print(image_info[\"width\"])  # 400\n",
    "print(image_info[\"isPNG\"])  # True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7590f183-d4fe-43dd-a3a9-d66dc9848a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_info[\"size\"] = 1024\n",
    "print(image_info[\"size\"])  # 1024\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfd7b292-ac31-4b1a-a504-7858ea49876e",
   "metadata": {},
   "source": [
    "## 8. 练习for循环的操作、判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efdf6234-dda1-496a-b2cc-98aaf49444e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 假设已经获得了所有图片的名称\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\", \"image5.png\"]\n",
    "num_images = len(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf0fdd81-1f9c-4a60-859d-8fdcf2a55367",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 进入循环\n",
    "for image_index in range(num_images):\n",
    "\n",
    "    # 图片的实际链接地址\n",
    "    name = image_names[image_index]\n",
    "    image_link = \"http://vis-www.cs.umass.edu/\" + name\n",
    "\n",
    "    # 只下载PNG图片\n",
    "    if name[-3:] == \"png\":\n",
    "        print(\"Download image: \" + image_link)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9879758a-fc10-42df-800d-d73c88c96699",
   "metadata": {},
   "source": [
    "## 练习另一种方式的for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3509463-4511-43d3-805f-ff716c81b2af",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 假设已经获得了所有图片的名称\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\", \"image5.png\"]\n",
    "num_images = len(image_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b9d2413-6e8b-4ab5-87a4-5660a0f8d896",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 进入循环\n",
    "for name in image_names:\n",
    "\n",
    "    # 图片的实际链接地址\n",
    "    image_link = \"http://vis-www.cs.umass.edu/\" + name\n",
    "\n",
    "    # 只下载PNG图片\n",
    "    if name[-3:] == \"png\":\n",
    "        print(\"Download image: \" + image_link)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "967600e4-06e7-460f-add1-949077aeb0ea",
   "metadata": {},
   "source": [
    "## 练习for循环的中止"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5c956b2-8c21-4df2-90e6-9b35799f0989",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\", \"image5.png\"]\n",
    "num_images = len(image_names)\n",
    "\n",
    "# 进入循环\n",
    "for image_index in range(num_images):\n",
    "\n",
    "    # 图片的实际链接地址\n",
    "    name = image_names[image_index]\n",
    "    image_link = \"http://vis-www.cs.umass.edu/\" + name\n",
    "\n",
    "    # 控制是否终止当前步或后继所有步的循环，continue 终止当前步，break 终止后继所有步的循环\n",
    "    if (image_index == 3):\n",
    "        continue\n",
    "        # 可以尝试将continue换成break来看差别\n",
    "\n",
    "    # 只下载PNG图片\n",
    "    if name[-3:] == \"png\":\n",
    "        print(\"Download image: \" + image_link)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7493ae81-00bf-46ba-be82-fd11448bcb50",
   "metadata": {},
   "source": [
    "## 练习while循环的操作、逻辑操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2eafccd-2999-41eb-9c96-f4e3ec299ac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\", \"image5.png\"]\n",
    "image_count = 0\n",
    "\n",
    "while (image_count < 4):\n",
    "    # 获取图片名称\n",
    "    name = image_names[image_count]\n",
    "    image_count = image_count + 1\n",
    "\n",
    "    # 图片的实际链接地址\n",
    "    image_link = \"http://vis-www.cs.umass.edu/\" + name\n",
    "\n",
    "    # 只下载PNG和JPG图片\n",
    "    if (name[-3:] == \"png\") or (name[-3:] == \"jpg\"):\n",
    "        print(\"Download image \" + str(image_count) + \": \" + image_link)\n",
    "    else:\n",
    "        print(\"Cancel image \" + str(image_count) + \": \" + image_link)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a7976db-083a-44c4-9192-97c0ea093d41",
   "metadata": {},
   "source": [
    "## 练习while循环和输入操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d8a98b3-79dc-4233-8c51-60b17b39ce51",
   "metadata": {},
   "outputs": [],
   "source": [
    "loop_count = 1\n",
    "\n",
    "while True:\n",
    "    user_type = input(\"请输入任何内容并按回车：\")\n",
    "    if user_type == \"quit\":\n",
    "        print(\"退出循环！\")\n",
    "        break\n",
    "    elif user_type == \"help\":\n",
    "        print(\"请输入任何内容并按回车\")\n",
    "        print(\"输入 quit 停止 \")\n",
    "        print(\"输入 help 获取帮助信息\")\n",
    "    else:\n",
    "        print(\"你的输入内容是：\" + user_type)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "406bcf2f-2b18-4ac5-bbc1-2acc31657fbb",
   "metadata": {},
   "source": [
    "## 9. 练习自定义函数文件的导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4de896d1-fd67-493e-b9a9-9673096bc27f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imageFuncs.py文件中包含自定义的函数 download_images\n",
    "import imageFuncs\n",
    "\n",
    "image_names = [\n",
    "    \"image1.png\",\n",
    "    \"image2.png\",\n",
    "    \"image3.jpg\",\n",
    "    \"image4.gif\",\n",
    "    \"image5.png\"]\n",
    "image_max = 4\n",
    "onlyPNG = True\n",
    "\n",
    "imageFuncs.download_images(image_names, image_max, onlyPNG)\n",
    "\n",
    "image_names_more = [\"pic1.png\", \"pic2.png\", \"pic3.jpg\", \"pic4.gif\", \"pic5.png\", \"pic6.png\"]\n",
    "image_max2 = 5\n",
    "onlyPNG = False\n",
    "\n",
    "imageFuncs.download_images(image_names_more, image_max2, onlyPNG)\n",
    "\n",
    "# 假设已经获得了所有图片的名称\n",
    "print(\"\\nNo function call\")\n",
    "image_names = [\"image1.png\", \"image2.png\", \"image3.jpg\", \"image4.gif\", \"image5.png\"]\n",
    "\n",
    "# 进入循环\n",
    "for name in image_names:\n",
    "\n",
    "    # 图片的实际链接地址\n",
    "    image_link = \"http://vis-www.cs.umass.edu/\" + name\n",
    "\n",
    "    # 只下载PNG图片\n",
    "    if name[-3:] == \"png\":\n",
    "        print(\"Download image: \" + image_link)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106dc495-b6c1-4909-9ae7-39126f376d67",
   "metadata": {},
   "source": [
    "## 10. 练习库的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46ad76bd-895e-4b1c-abbd-b45cf521005f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用数学函数库\n",
    "# 导入math库，你也可以把这句放在文件最前面，就可以不用在每个函数中导入了\n",
    "import math\n",
    "\n",
    "a = 3\n",
    "b = 4\n",
    "c = math.sqrt(a * a + b * b)\n",
    "pi3 = math.pi / 3.0\n",
    "cos_pi3 = math.cos(pi3)\n",
    "print(\"pi/3 = \", pi3)\n",
    "print(\"cos_(pi/3) = \", cos_pi3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f828924-4bd2-4547-a389-425173d8dd93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用图像类库（在Jupyter中无法显示图像）\n",
    "from PIL import Image\n",
    "\n",
    "aImage = Image.open(\"image_download/image_004.png\")\n",
    "aImage = aImage.resize((180, 180), Image.ANTIALIAS)\n",
    "aImage = aImage.crop((50, 50, 130, 130))\n",
    "aImage = aImage.rotate(45)\n",
    "aImage.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d871d407-b2d9-421e-898b-c86fcff34591",
   "metadata": {},
   "source": [
    "## 11. 练习numpy库的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27c26889-6247-4a93-952b-6fb627c14d0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入numpy\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([(1.5, 2, 3), (4, 5, 6)])\n",
    "print(\"\\n Numpy Array:\")\n",
    "print(a)  # 自动赋予元素实数属性\n",
    "print(\"a[1, 0] = \" + str(a[1, 0]))\n",
    "\n",
    "b = [[1.5, 2, 3], [4, 5, 6]]\n",
    "print(\"\\n Python List:\")\n",
    "print(b)  # 和numpy的操作有所不同\n",
    "print(\"b[1][0] = \" + str(b[1][0]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1695e861-059c-4b1a-8ccf-d98b9ef8fd42",
   "metadata": {},
   "source": [
    "## 练习numpy来操作数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38ea325e-1a63-4626-840b-17edee0d0d0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入numpy\n",
    "import numpy as np\n",
    "\n",
    "# 在20到145之间产生5个数字组成的等差数列\n",
    "time = np.linspace(20, 145, 5)\n",
    "print(\"\\ntime = \")\n",
    "print(time)\n",
    "\n",
    "# 产生0~19的数列，变成5*4，然后每个元素求sin()\n",
    "data = np.sin(np.arange(20)).reshape(5, 4)\n",
    "print(\"\\ndata = \")\n",
    "print(data)\n",
    "# 保存数据\n",
    "np.savetxt(\"data.txt\", data, fmt='%5.2f ')  # 保存数据，设置格式\n",
    "\n",
    "# 导入数据\n",
    "data_import = np.loadtxt(\"data.txt\")\n",
    "print(\"\\ndata_import = \")\n",
    "print(data_import)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a5a2809-429f-4d32-bbb5-e911065ce4bb",
   "metadata": {},
   "source": [
    "## 12. 练习函数调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "942e58a4-7198-4cac-8d40-5db84f6fa51e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def download_images(image_names, max_num=5, onlyPNG=True):\n",
    "    img_num = 1\n",
    "    for name in image_names:\n",
    "        if onlyPNG and (name[-3:] == 'png'):\n",
    "            print('Download PNG image: ' + name)\n",
    "        else:\n",
    "            print('Download image: ' + name)\n",
    "        img_num += 1\n",
    "        if img_num > max_num:\n",
    "            break\n",
    "\n",
    "image_names = ['a1.jpg', 'a2.png', 'a3.png', 'a4.png', 'a5.gif']\n",
    "\n",
    "# 提供所有参数的值\n",
    "download_images(image_names, 4, False)\n",
    "\n",
    "# 只提供前1个参数的值，其它参数取默认值\n",
    "download_images(image_names)\n",
    "\n",
    "# 只提供前两个参数的值，第3个参数取默认值\n",
    "download_images(image_names, 3)\n",
    "\n",
    "# 跳过第2个参数，设置第3个参数的值\n",
    "download_images(image_names, onlyPNG=False)\n",
    "\n",
    "# 用元组的形式调用参数\n",
    "params = (image_names, 2, True)\n",
    "download_images(*params)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84c6b70b-aacc-4f14-b855-f8a23b8b6f48",
   "metadata": {},
   "source": [
    "## 13. 变量存在范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "258bcb14-a481-477b-ab60-a75550c78bef",
   "metadata": {},
   "outputs": [],
   "source": [
    "def str_change(aStr, bStrList):\n",
    "    aStr = aStr.upper()\n",
    "    bStrList[1] = 'Python'\n",
    "    print('aStr in str_change(): ' + aStr)\n",
    "    return aStr, bStrList\n",
    "\n",
    "aStr = 'Hello'\n",
    "bStrList = ['World', 'Ai']\n",
    "\n",
    "str_change(aStr, bStrList)\n",
    "print('aStr = ' + aStr)  # 数字、字符、字符串、元组等不可变对象，在函数内改变值后，函数结束仍为原值。\n",
    "print('bStrList = ' + str(bStrList))  # 列表、字典等可变对象，在函数内改变值后，函数结束后改变值。\n",
    "\n",
    "rst1 = str_change(aStr, bStrList)  # 返回的变量值变了\n",
    "print(rst1)  # 返回到同一元组\n",
    "print('aStr = ' + aStr)  # 字符串变量仍不变\n",
    "\n",
    "rst1, rst2 = str_change(aStr, bStrList)  # 返回到两个变量中\n",
    "print('rst1 = ' + rst1)\n",
    "print('rst2 = ' + str(rst2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
